package com.sg.service.base.framework.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.framework.req.DeletePositionTitleReq;
import com.sg.dto.base.framework.req.QueryPositionTitleDetailReq;
import com.sg.dto.base.framework.req.QueryPositionTitleListReq;
import com.sg.entity.PositionIdName;
import com.sg.service.base.framework.MPositionIdNameService;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.dal.mapper.mbg.PositionIdNameMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MPositionIdNameServiceImpl
        extends ServiceImpl<PositionIdNameMapper, PositionIdName>
        implements MPositionIdNameService {

    @Autowired
    PositionIdNameMapper positionIdNameMapper;

    /**
     * 1-3-06查询职务名称列表[152]
     * gen by moon at 10/16/2022, 11:07:24 AM
     */
    @Trace(operationName = "1-3-06查询职务名称列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<PositionIdName> queryPositionTitleList(QueryPositionTitleListReq reqDto) {

        try {
            QueryWrapper<PositionIdName> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(PositionIdName.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(PositionIdName.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(PositionIdName.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<PositionIdName>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-3-06查询职务名称[151]
     * gen by moon at 10/16/2022, 11:07:24 AM
     */
    @Trace(operationName = "1-3-06查询职务名称")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PositionIdName queryPositionTitleDetail(QueryPositionTitleDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<PositionIdName> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<PositionIdName> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                PositionIdName retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-3-06删除职务名称[150]
     * gen by moon at 10/16/2022, 11:07:23 AM
     */
    @Trace(operationName = "1-3-06删除职务名称")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deletePositionTitle(DeletePositionTitleReq reqDto) {

        try {
            Map<String, Object> columnMap = CommonFunctionHelper.objectToMap(reqDto);
            return super.removeByMap(columnMap);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-3-06修改职务名称[149]
     * gen by moon at 10/16/2022, 11:07:23 AM
     */
    @Trace(operationName = "1-3-06修改职务名称")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updatePositionTitle(PositionIdName positionIdName) {
        positionIdName.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (positionIdName != null && positionIdName.getSubjectLifeCycle() != null && positionIdName.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            positionIdName.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        positionIdName.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (positionIdName != null && positionIdName.getSubjectLifeCycle() != null && positionIdName.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            positionIdName.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        return super.updateById(positionIdName);
    }

    /**
     * 1-3-06新增职务名称[148]
     * gen by moon at 10/16/2022, 11:07:22 AM
     */
    @Trace(operationName = "1-3-06新增职务名称")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addPositionTitle(PositionIdName positionIdName) {
        positionIdName.setPositionIdNameId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        positionIdName.setIsArchive("FALSE");
        positionIdName.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        positionIdName.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (positionIdName != null && positionIdName.getSubjectLifeCycle() != null && positionIdName.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            positionIdName.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        positionIdName.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        positionIdName.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (positionIdName != null && positionIdName.getSubjectLifeCycle() != null && positionIdName.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            positionIdName.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        positionIdName.setIsValid("FALSE");
        positionIdName.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        positionIdName.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(positionIdName)) {
            return positionIdName.getPositionIdNameId();
        } else {
            return "-1";
        }

    }

    /**
     * M1查询职务名称测试列表[4908]
     * gen by moon at 10/16/2022, 11:07:25 AM
     */
    @Trace(operationName = "M1查询职务名称测试列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<PositionIdName> queryPositionIdNameTestDataList(PositionIdName positionIdName) {

        try {
            QueryWrapper<PositionIdName> queryWrapper = new QueryWrapper<>();


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(positionIdName);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(PositionIdName.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(PositionIdName.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(PositionIdName.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<PositionIdName>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * M1批量删职务名称测试[4910]
     * gen by moon at 10/16/2022, 11:07:26 AM
     */
    @Trace(operationName = "M1批量删职务名称测试")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeletePositionIdNameTestData(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }
}
